<template>
  <ex-dialog
    width="800px"
    class="ex-diolog"
    :title="title"
    :visible="visible"
    :close-on-click-modal="false"
    @closeDialog="handleClose"
  >
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="积分发放方式：" prop="grantMethod">
              <el-select filterable v-model="ruleForm.grantMethod" placeholder="请选择" clearable
                :style="{ width: inputWidth }">
                <el-option v-for="item in statusList" :key="item.id" :label="item.value" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="积分类型：" prop="integralType">
              <el-select filterable v-model="ruleForm.integralType" placeholder="请选择" clearable
                :style="{ width: inputWidth }">
                <el-option v-for="item in integralTypeList" :key="item.id" :label="item.value" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
       
        <el-row>
          <el-col :span="12">
            <el-form-item label="发放数额：" prop="grantValue">
              <el-input v-model="ruleForm.grantValue" placeholder="请填写数额" maxlength="50">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
       
      </el-form>
    </div>
    <div class="button_box">
      <el-button
        type="primary"
        :loading="AgreeLoading"
        @click="submitForm('ruleForm')"
      >保存提交</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
import Loading from "@/components/Loading/index";
// api
import {
  pointsRule,
  pointsBatchRule,
  pointsDetail,
} from "@/api/integralOperation/jtkConfiguration";
import '@wangeditor/editor/dist/css/style.css'
import {
  Editor,
  Toolbar
} from "@wangeditor/editor-for-vue";
export default {
  components: {
    Loading,
    Editor,
    Toolbar
  },
  props: {
    visible: Boolean,
    id: String,
    ids: Array,
    pathType: String,
  },
  data() {
    var checkNum = (rule, value, callback) => {
      if (value < 0) {
        return callback(new Error('只可填写数值（0-100），可保留两位小数'));
      } else if (value > 100) {
        return callback(new Error('只可填写数值（0-100），可保留两位小数'));
      } else {
        callback();
      }
    };
    return {
      labelWidth: "150px",
      inputWidth: "420px",
      title: "编辑配置",
      loading: false,
      disabled: false,
      AgreeLoading: false,
      ruleForm: {
        id: '',
        idList: [],
        grantMethod: "", //积分发放方式
        integralType:'',//积分类型
        grantValue: "", //发放数额
      },
      statusList: [],//店铺下拉列表
      integralTypeList: [],//店铺活动资产下拉
      rules: {
        grantMethod: [{
          required: true,
          message: '请选择积分发放方式',
          trigger: ['blur', 'change']
        }],
        integralType: [{
          required: true,
          message: '请选择积分类型',
          trigger: ['blur', 'change']
        }],
        grantValue: [
          { required: true,message: "请填写数额",trigger: ["change", "bulr"]},
          {
            pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
            message: '只可填写数值（0-100），可保留两位小数'
          },
          { validator: checkNum }
        ],
      
       
      },
    };
  },
  computed: {},
  created() {
    this.getDict()
    if(this.pathType == 'edit'){
      this.title = '编辑配置'
      this.ruleForm.id = this.id
      this.getInfo()
    }else if(this.pathType == 'add'){
      this.title = '批量配置'
      this.ruleForm.idList = this.ids
    }
  },
  mounted() {
   
  },
  computed: {},
  methods: {
    //获取字典
    getDict() {
      var dictType = "jutuike_integral_grant_method,jutuike_integral_type";
      this.$getSelectList(dictType)
        .then((res) => {
          var arr = [{
            id: "",
            value: "全部",
          }, ];
          this.statusList = res.result[0].dictList
          this.integralTypeList = res.result[1].dictList;
        })
        .finally(() => {

        });
      },
    //获取详情
    getInfo(){
      var id = this.ruleForm.id
      pointsDetail(id).then(res => {
        this.ruleForm = {
          ...res.result,
        }
      })
    },
    
    //取消
    handleClose(formName) {
      this.$emit("update:visible", false);
    },
    successImg(e) {
      this.ruleForm.qrcode = e
      this.$refs['imgRef'].clearValidate();
    },
    // 删除图片
    handleRemove(){
      this.ruleForm.qrcode = ''
    },
    getTime(e){
      console.log("e",e)
      this.ruleForm.startTime =  e[0] 
      this.ruleForm.endTime = e[1]
      console.log("e",this.ruleForm.startTime)
      console.log("e",this.ruleForm.endTime)
    },
    
    submitForm(formName) {
      console.log(this.ruleForm);
      //创建确定
      this.$refs[formName].validate(valid => {
        let isCheck = true;
        let isStock = true;
        let isAmount = true;
        if (valid) {
          this.AgreeLoading = true
          
          var data = {
            ...this.ruleForm
          }
          if(this.pathType == 'add'){
            pointsBatchRule(data).then(res => {
              setTimeout(() => {
                this.handleClose()
                this.$message.success('配置成功')
                this.$emit('success')
                this.AgreeLoading = false
              }, 1000);
            }).finally(()=>{
              this.AgreeLoading = false
            })
          }else if(this.pathType == 'edit'){
            pointsRule(data).then(res => {
              setTimeout(() => {
                this.handleClose()
                this.$message.success('配置成功')
                this.$emit('success')
                this.AgreeLoading = false
              }, 1000);
            }).finally(()=>{
              this.AgreeLoading = false
            })
          }
          
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
    padding: 0 !important;
  }

  ::v-deep .ex--dialog--main {
    padding: 10px 20px !important;
  }
}

.main {
  padding: 20px 20px;
}
.button_box{
  display: flex;
  justify-content: center;
}
</style>
